Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Prevent UnionTransformer type ambiguity in combination with PyTorchTypeTransformer #2726

Merged
merged 2 commits into from
Sep 14, 2024

Conversation

fg91
Copy link
Member

@fg91 fg91 commented Sep 2, 2024

Why are the changes needed?

@task
def foo() -> Union[list[torch.Tensor], torch.Tensor]:
    return [torch.tensor([1, 2, 3])]


@workflow
def wf():
    foo()

This example fails with:

TypeError: Failed to convert outputs of task 'wf.foo' at position 0.
Failed to convert type <class 'list'> to type typing.Union[list[torch.Tensor], torch.Tensor].
Error Message: Ambiguous choice of variant for union type.

The type transformer choice should, however, not be ambiguous.

The problem arises as follows:

Here in the UnionTransformer we test all union variants and raise the observed error if more than one of the union variants work.

In this case, the two union working variants are:

  • Call the ListTransformer for a list of tensors and then the PyTorchTensorTransformer for each element in the list. This is the expected behaviour and does work.
  • Call the PyTorchTensorTransformer for the list of tensors. This, however, works as well as torch.save([some_tensor], ...) works. This is not intended and hence causes the ambiguity.

What changes were proposed in this pull request?

We need to prevent the pytorch type transformers from serializing lists of tensors or modules since the type engine (i.e. the UnionTransformer expects these cases to be handled by the ListTransformer).

How was this patch tested?

Tested with the snippet above and added a unit test that would have caught this.

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

@fg91 fg91 added the bug Something isn't working label Sep 2, 2024
@fg91 fg91 self-assigned this Sep 2, 2024
Copy link

codecov bot commented Sep 2, 2024

Codecov Report

Attention: Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.

Project coverage is 71.89%. Comparing base (8bad8e6) to head (99df4fe).
Report is 16 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/extras/pytorch/native.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #2726       +/-   ##
===========================================
+ Coverage   49.76%   71.89%   +22.13%     
===========================================
  Files         193      193               
  Lines       19659    19683       +24     
  Branches     4097     4101        +4     
===========================================
+ Hits         9783    14152     +4369     
+ Misses       9345     4841     -4504     
- Partials      531      690      +159     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we include one more unit test based on your example in #2726 (comment) to make sure this continues to work?

@fg91
Copy link
Member Author

fg91 commented Sep 9, 2024

@workflow
def wf():
    foo()

Absolutely 99df4fe

@fg91 fg91 requested a review from thomasjpfan September 9, 2024 21:50
@fg91
Copy link
Member Author

fg91 commented Sep 11, 2024

@thomasjpfan are you ok with merging this PR?

@thomasjpfan thomasjpfan merged commit 0b26c92 into master Sep 14, 2024
102 of 103 checks passed
@thomasjpfan thomasjpfan deleted the fg91/fix/torch-type-transformer-ambiguous-union branch September 14, 2024 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants